package io.lkt.modules.sys.controller;

import com.google.common.collect.Lists;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BPosterDetailsEntity;
import io.lkt.modules.sys.entity.BPosterInfoEntity;
import io.lkt.modules.sys.entity.SysGzhEntity;
import io.lkt.modules.sys.service.BPosterDetailsService;
import io.lkt.modules.sys.service.BPosterInfoService;
import io.lkt.modules.sys.service.SysUserRoleService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 海报信息表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2020-12-01 16:25:18
 */
@RestController
@RequestMapping("/sys/bposterinfo")
public class BPosterInfoController {
    @Autowired
    private BPosterInfoService bPosterInfoService;

    @Autowired
    private BPosterDetailsService bPosterDetailsService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:bposterinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = bPosterInfoService.queryPage(params);

        List<?> list = page.getList();

        if (CollectionUtils.isNotEmpty(list)) {
            List<BPosterInfoEntity> bPosterInfoList = Lists.newArrayList();

            for (Object temp : list) {
                BPosterInfoEntity bPosterInfoEntity = (BPosterInfoEntity) temp;
                //公众号名称
                if ((bPosterInfoEntity.getGzhCode() != null) && (!bPosterInfoEntity.getGzhCode().equals(""))) {
                    SysGzhEntity sysGzh = this.sysUserRoleService.getGzhByCode(bPosterInfoEntity.getGzhCode());
                    bPosterInfoEntity.setGzhName(sysGzh.getGzhName());
                } else {
                    bPosterInfoEntity.setGzhName("全部平台");
                }

                bPosterInfoList.add(bPosterInfoEntity);
            }
            page.setList(bPosterInfoList);
        }

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:bposterinfo:info")
    public R info(@PathVariable("id") Integer id) {
        BPosterInfoEntity bPosterInfo = bPosterInfoService.getById(id);

        if (bPosterInfo.getGzhCode() == null || bPosterInfo.getGzhCode().equals("")) {
            bPosterInfo.setGzhCode("-1");
        }

        return R.ok().put("bPosterInfo", bPosterInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:bposterinfo:save")
    public R save(@RequestBody BPosterInfoEntity bPosterInfo) {

        if (bPosterInfo.getGzhCode() == null || bPosterInfo.getGzhCode().equals("") || bPosterInfo.getGzhCode().equals("-1")) {
            bPosterInfo.setGzhCode("");
        }

        bPosterInfo.setCreateTime(new Date());
        bPosterInfo.setUpdateTime(new Date());
        bPosterInfoService.save(bPosterInfo);

        // 保存底图到详情表
        if (bPosterInfo != null && bPosterInfo.getBase() != null && StringUtils.isNotBlank(bPosterInfo.getBase())) {
            BPosterDetailsEntity bPosterDetails = new BPosterDetailsEntity();
            bPosterDetails.setType(bPosterInfo.getId());
            bPosterDetails.setItem(1011);
            bPosterDetails.setRemark("海报底图");
            bPosterDetails.setImange(bPosterInfo.getBase());
            bPosterDetails.setWidth(750);
            bPosterDetails.setHeight(750);
            bPosterDetails.setStatus(1);

            bPosterDetails.setCreateTime(new Date());
            bPosterDetails.setUpdateTime(new Date());

            bPosterDetailsService.save(bPosterDetails);
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:bposterinfo:update")
    public R update(@RequestBody BPosterInfoEntity bPosterInfo) {
        ValidatorUtils.validateEntity(bPosterInfo);

        if (bPosterInfo.getGzhCode() == null || bPosterInfo.getGzhCode().equals("") || bPosterInfo.getGzhCode().equals("-1")) {
            bPosterInfo.setGzhCode("");
        }

        bPosterInfo.setUpdateTime(new Date());
        bPosterInfoService.updateById(bPosterInfo);

        // 更新底图到详情表
        BPosterDetailsEntity bPosterDetailsEntity = new BPosterDetailsEntity();
        bPosterDetailsEntity.setType(bPosterInfo.getId());
        bPosterDetailsEntity.setItem(1011);
        bPosterDetailsEntity.setStatus(1);
        List<BPosterDetailsEntity> bPosterDetailsList = bPosterDetailsService.queryAll(bPosterDetailsEntity);

        // 更新海报详情为删除状态
        if (CollectionUtils.isNotEmpty(bPosterDetailsList)) {
            for (BPosterDetailsEntity info : bPosterDetailsList) {
                info.setImange(bPosterInfo.getBase());
                info.setUpdateTime(new Date());
                bPosterDetailsService.updateById(info);
            }
        } else if (bPosterInfo != null && bPosterInfo.getBase() != null && StringUtils.isNotBlank(bPosterInfo.getBase())) {
            // 保存底图到详情表
            BPosterDetailsEntity info = new BPosterDetailsEntity();
            info.setType(bPosterInfo.getId());
            info.setItem(1011);
            info.setRemark("海报底图");
            info.setImange(bPosterInfo.getBase());
            info.setWidth(750);
            info.setHeight(750);
            info.setStatus(1);

            info.setCreateTime(new Date());
            info.setUpdateTime(new Date());

            bPosterDetailsService.save(info);
        }

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:bposterinfo:delete")
    public R delete(@RequestBody Integer[] ids) {
        bPosterInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
